#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libnotify/notify.h>
#include <gtk/gtk.h>
#include <time.h>
#include <libintl.h>
#include <locale.h>
#include <glib.h>

#define _(string) gettext(string)

int get_systemname(char *name)
{
  char mystring[50];
  char systemname[15];
  FILE *rf;
  rf=fopen("/etc/lsb-release","r");
  if(rf == NULL){
    printf("open /etc/lsb-release fail!");
    return -1;
  }
  fgets(mystring,50,rf);
  fclose(rf);
  strncpy(systemname,mystring+11,strlen(mystring)-11);
  systemname[strlen(mystring)-11]='\0';
  if(strcmp(systemname,name) == 0)
  {
    return 0;
  }
  return -1;
}

static gboolean is_chinese_system (void)
{
    const char * const *langs_pointer;
    int i;
	gboolean bIsChinese = FALSE;

    langs_pointer = g_get_language_names ();
    for (i = 0; langs_pointer[i] != NULL; i++)
    {
        if(0 == strcmp(langs_pointer[i],"zh"))
		{
			bIsChinese = TRUE;
			break;
		}
    }
    return bIsChinese;
}

static void actionstatement(NotifyNotification *n, gchar *action, gpointer data)
{
	if(TRUE == is_chinese_system())
	{
    	system("atril /usr/share/man/statement.pdf.gz");
	}
	else
	{
		system("atril /usr/share/man/statement_en.pdf.gz");
	}
}

static void action(NotifyNotification *n, gchar *action, gpointer data){
system("/usr/bin/ukui-about");
}

static void action_activation(NotifyNotification *n, gchar *action, gpointer data){
system("/usr/bin/kylin-activation");
}

int isshownotify(){
	time_t system_time 	= 0;
	long int lasttime	= 0;
	FILE *date_fp 		= NULL;
	FILE *status_fp 	= NULL;
	char *statusfile 	= NULL;
	char *datefile 		= NULL;
	int  term_status 	= NULL;  
	int  iRetVal 		= 0;

  	do
	{
		time(&system_time);
		const char* homepath = g_get_home_dir();
		if(homepath==NULL||0 == strlen(homepath))
		{
			iRetVal = 1;
			break;
		}

		statusfile = (char *)malloc(strlen(homepath)+strlen("/.config/k_status")+1);
		if(NULL == statusfile)
		{
			iRetVal = 2;
			break;
		}
		datefile = (char *)malloc(strlen(homepath)+strlen("/.config/k_date")+1);
		if (NULL == datefile)
		{
			iRetVal = 2;  
			break;
		}

		strcpy(statusfile, homepath);  
		strcat(statusfile, "/.config/k_status");
		strcpy(datefile, homepath);  
		strcat(datefile, "/.config/k_date");
		status_fp=fopen(statusfile,"r");
		if(status_fp==NULL) 
		{
			iRetVal = -1;
			break;
		}
		
		term_status = fgetc(status_fp);
		fclose(status_fp);
		status_fp = NULL;
		if(EOF == term_status)
		{
			iRetVal = -1;
			break;
		}
		
		if(term_status=='q') 
		{
			date_fp=fopen(datefile,"w+");
			if(date_fp==NULL) 
			{
				iRetVal = -2;
				break;
			}
			fprintf(date_fp,"%ld",system_time);
			fclose(date_fp);
			date_fp= NULL;
			iRetVal = 0;
			break;
		}
		else if(term_status=='e' || term_status=='i' || term_status=='n' || term_status=='s')
		{
			iRetVal = -3;
			break;
		}
		else if(term_status=='a')
		{
			iRetVal = 5;
			break;
		}

		date_fp=fopen(datefile,"r+");
		if(date_fp==NULL)
		{
			date_fp=fopen(datefile,"w+");
			if(date_fp==NULL) 
			{
				iRetVal = 3;
				break;
			}
		} 
		int tmp;  
		tmp=fscanf(date_fp,"%ld",&lasttime);
		rewind(date_fp);
		if(tmp==1){
		if(system_time-lasttime>604800||system_time<lasttime)
		{
			fprintf(date_fp,"%ld",system_time);
			fclose(date_fp);
			date_fp = NULL;
			iRetVal = 0;  
			break;
		}
		else 
		{
			fclose(date_fp);
			date_fp = NULL;
			iRetVal = 4;
			break;
		}
		}
		fprintf(date_fp,"%ld",system_time);
		fclose(date_fp);
		date_fp = NULL;
	}while(0);

	if(NULL != statusfile)
	{
		free(statusfile);
		statusfile = NULL;
	}
	if(NULL != datefile)
	{
		free(datefile);
		datefile = NULL;
	}
	return iRetVal;
}

int main(int argc, char **argv){

  setlocale(LC_ALL,"");
  bindtextdomain("kylin-verify","/usr/share/locale");
  bind_textdomain_codeset("kylin-verify","UTF-8");
  textdomain("kylin-verify");

  daemon(1,1);
  int num=1;
  do{
  gtk_init(&argc, &argv);
    //notify_init("MyApp");
	 NotifyNotification *kynotify = NULL;
  char *summary;
  if(get_systemname("YHKylin\n") == 0){
     summary = _("Thank you for taking the Kylin");
  }
  else if(get_systemname("Kylin\n") == 0)
     summary = _("Thank you for taking the Kylin");
  else if(get_systemname("NeoKylin\n") == 0)
     summary = _("Thank you for taking the neokylin");
  else
     summary = _("Thank you for taking the Kylin");
  char *error_str =_("Validation program error!");
  char message[256] = "";
  char message_all[1024] = "";
  int flag=0;
  int isshow=0;
  FILE *kyfd;

  kyfd = popen("/usr/bin/kylin-verify" ,"r");
  while(fgets(message, sizeof(message), kyfd)!=NULL){
	  strncat(message_all,message,strlen(message));
	  flag=1;
  }
  pclose(kyfd);
  isshow=isshownotify();
  if(isshow>0) return EXIT_SUCCESS;
  if(isshow<0 || num==1){
  if(flag==0){
	strncpy(message_all,error_str,strlen(error_str));
	}
  notify_init("kylin verify gui");
  kynotify = notify_notification_new (
			summary,
			message_all,
                        "yhkylin");
  notify_notification_set_timeout (kynotify,10*1000);
  notify_notification_add_action(
        kynotify, "statement", _("Disclaimer for trial version"), (NotifyActionCallback)actionstatement, NULL, NULL);
  notify_notification_add_action(
        kynotify, "activation", _("License activation"), (NotifyActionCallback)action_activation, NULL, NULL);
  notify_notification_add_action(
        kynotify, "ukui-about", _("The detailed information"), (NotifyActionCallback)action, NULL, NULL);  

  g_signal_connect(G_OBJECT(kynotify),"closed",G_CALLBACK(gtk_main_quit),NULL);
  notify_notification_show (kynotify, NULL);
  gtk_main();
  g_object_unref(G_OBJECT(kynotify));
  notify_uninit();
  num++;
  }
  sleep(600*3);
  }while(num>0);
  return EXIT_SUCCESS;
}

